package com.vichat.common.sms;


import com.alibaba.fastjson.JSONObject;
import com.vichat.common.vo.Json;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.HashMap;
import java.util.Map;


/**
 * Created by dongql on 15/12/2015.
 */
public class SmsUtil {

    private static Logger logger = LoggerFactory.getLogger(SmsUtil.class);

    public static final String apikey = "d576f2e63e627054ffd14b40cc5b2bd6";


    private static String defaultWarnMobile = "18802106866";
    


	public static Json sendSms(String mobile, String smsText) {
		Json result = new Json();
		try {
			String data = JavaSmsApi.sendSms(apikey, smsText, mobile);
			logger.info(mobile + "->" + smsText + " : " + data);
			JSONObject json = JSONObject.parseObject(data);
			logger.info(json.toString());
			int code = json.getIntValue("code");
			result.setObj(code);
			if (code != 0) {
				String msg = json.getString("msg");
				result.setMsg(msg);
				result.setSuccess(false);
			} else {
				result.setSuccess(true);
			}
		} catch (Exception e) {
			result.setMsg(e.getMessage());
			result.setSuccess(false);
		}
		return result;
	}

    private static String getSmsContent(final String template, Map<String, String> params) throws Exception {
        String t = template;
        for (String param : params.keySet()) {
            String key = "#" + param + "#";
            String code = params.get(param);
            t = t.replaceAll(key, code);
        }
        if (t.indexOf("#") > 0) {
            throw new Exception("短信模板有未解析字段");
        }
        return t;
    }

    /**
     * 发送短信验证码
     * @param mobile
     * @param code
     * @return
     * @throws Exception
     */
    public static Json sendCode(final String mobile,String code) throws Exception {
        String template = "【微喂网络】您的验证码是#code#。如非本人操作，请忽略本短信";
         Map<String, String> params = new HashMap<>();
        params.put("code", code);
        final String text = SmsUtil.getSmsContent(template, params);
        return SmsUtil.sendSms(mobile, text);
    }

    /**
     * 用户密码验证码提醒短信
     * @param mobile
     * @param code
     * @param appUrl
     * @return
     * @throws Exception
     */
    public static Json sendPwdCode(final String mobile,String code,String appUrl)throws Exception {
        String template = "【微喂网络】您的App登陆用户：#mobile#密码：#code#，App下载地址：#url#。如非本人操作，请忽略本短信";
        Map<String, String> params = new HashMap<>();
        params.put("mobile", mobile);
        params.put("code", code);
        params.put("url",appUrl);
        final String text = SmsUtil.getSmsContent(template, params);
        return SmsUtil.sendSms(mobile, text);
    }

    /**
     * App下载提醒短信
     * @param mobile
     * @param url
     * @return
     * @throws Exception
     */
    public static Json sendAppUrl(final String mobile,String url)throws Exception {
        String template = "【微喂网络】App下载地址：#url#。如非本人操作，请忽略本短信";
        Map<String, String> params = new HashMap<>();
        params.put("url", url);
        final String text = SmsUtil.getSmsContent(template, params);
        return SmsUtil.sendSms(mobile, text);
    }

    /**
     * 服务到期提醒短信
     * @param mobile
     * @param day
     * @return
     * @throws Exception
     */
    public static Json sendOrderService(final String mobile,String day)throws Exception {
        String template = "【微喂网络】即聊服务还剩#day#天到期，请尽快登录平台续费，到期将停用服务。如非本人操作，请忽略本短信";
        Map<String, String> params = new HashMap<>();
        params.put("day", day);
        final String text = SmsUtil.getSmsContent(template, params);
        return SmsUtil.sendSms(mobile, text);
    }

    public static void sendWarningInfo(String info, String... mobile) throws Exception {
        try{
            if(mobile == null || mobile.length == 0) {
                mobile = new String[1];
                mobile[0] = defaultWarnMobile;
            }
            String template = "【微喂网络】系统异常告警:#info#";
            Map<String, String> params = new HashMap<>();
            params.put("info", info);
            String text = SmsUtil.getSmsContent(template, params);
            for (String m : mobile)
                SmsUtil.sendSms(m, text);
        }catch(Exception e){
            logger.error("发送告警信息异常",e);
        }
    }

    /**
     * 代理商申请短信
     * @param mobile
     * @return
     * @throws Exception
     */
    public static Json sendAgentApply(final String mobile)throws Exception {
        String template = "【微喂网络】客服正在审核您的代理商申请，审核通过将会以短信的方式告知，请您关注短信提醒。如非本人操作，请忽略本短信。";
        Map<String, String> params = new HashMap<>();
        final String text = SmsUtil.getSmsContent(template, params);
        return SmsUtil.sendSms(mobile, text);
    }

    /**
     * 代理商审核通过短信
     * @param mobile
     * @return
     * @throws Exception
     */
    public static Json sendAgentAffirm(final String mobile)throws Exception {
        String template = "【微喂网络】代理商申请客服已审核通过，您可在微信公众号客户管理菜单添加客户。如非本人操作，请忽略本短信。";
        Map<String, String> params = new HashMap<>();
        final String text = SmsUtil.getSmsContent(template, params);
        return SmsUtil.sendSms(mobile, text);
    }

}
